﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace WPFExcelReport
{

    public partial class TWBH02 : ViewBase
    {
        #region ==FROMDATE===========
        public static readonly DependencyProperty FROMDATEProperty = DependencyProperty.Register("FROMDATE",
            typeof(string), typeof(TWBH02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnFROMDATEChanged));
        public string FROMDATE
        {
            get
            {
                return (string)GetValue(FROMDATEProperty);
            }
            set
            {
                SetValue(FROMDATEProperty, value);
            }
        }
        private static void OnFROMDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBH02;
            if (src != null)
            {
                src.FROMDATE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==ART===========
        public static readonly DependencyProperty ARTProperty = DependencyProperty.Register("ART",
            typeof(string), typeof(TWBH02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnARTChanged));
        public string ART
        {
            get
            {
                return (string)GetValue(ARTProperty);
            }
            set
            {
                SetValue(ARTProperty, value);
            }
        }
        private static void OnARTChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBH02;
            if (src != null)
            {
                src.ART = (string)e.NewValue;
                 
            }
        }
        #endregion 
        #region ==ENDDATE===========
        public static readonly DependencyProperty ENDDATEProperty = DependencyProperty.Register("ENDDATE",
            typeof(string), typeof(TWBH02),
            new FrameworkPropertyMetadata("", FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnENDDATEChanged));
        public string ENDDATE
        {
            get
            {
                return (string)GetValue(ENDDATEProperty);
            }
            set
            {
                SetValue(ENDDATEProperty, value);
            }
        }
        private static void OnENDDATEChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBH02;
            if (src != null)
            {
                src.ENDDATE = (string)e.NewValue;
            }
        }
        #endregion
        #region ==dwWBH===========
        public static readonly DependencyProperty dwWBHProperty = DependencyProperty.Register("dwWBH",
            typeof(DataRow), typeof(TWBH02),
            new FrameworkPropertyMetadata(null, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OndwWBHChanged));
        public DataRow dwWBH
        {
            get
            {
                return (DataRow)GetValue(dwWBHProperty);
            }
            set
            {
                SetValue(dwWBHProperty, value);
            }
        }
        private static void OndwWBHChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBH02;
            if (src != null)
            {
                src.dwWBH = (DataRow)e.NewValue;
            }
        }
        #endregion
        #region ==IsChkArt===========
        public static readonly DependencyProperty IsChkArtProperty = DependencyProperty.Register("IsChkArt",
            typeof(bool), typeof(TWBH02),
            new FrameworkPropertyMetadata(false, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnIsChkArtChanged));
        public bool IsChkArt
        {
            get
            {
                return (bool)GetValue(IsChkArtProperty);
            }
            set
            {
                SetValue(IsChkArtProperty, value);
            }
        }
        private static void OnIsChkArtChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWBH02;
            if (src != null)
            {
                src.IsChkArt = (bool)e.NewValue;
            }
        }
        #endregion
        VWBBST WB;
        ITXEXRPT ITXEXRPT;
        public TWBH02()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VWBFEHL();            
            WB = new VWBBST();
            IsMasterFromMV = true;
            TBMain = MV.SetProd.Tables[TB];
            InitializeComponent();

        }
        string filter = "";
        public override void Window_Loaded(object sender, System.Windows.RoutedEventArgs e)
        {
            base.Window_Loaded(sender, e);
            FROMDATE = Helpers.Helper.GetDateAddDays(MV.date, -7);
            ENDDATE = MV.date;
        }
        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
            dwWBH = dwMaster;
        }
        private void txtART_KeyUp(object sender, System.Windows.Input.KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                QueryCommand(null, null);
            }
        }
        public override void QueryCommand(object sender, System.Windows.RoutedEventArgs e)
        {

            DataSet Result;
            DataSet Return = new DataSet();
            DataRow dwData = null;
            ART = textART.Text.ToString().Trim();
            dwData = WB.SetProd.Tables["WBBST"].NewRow(); 
            dwData["WBBART"] = ART;
            dwData["WBBEINDAT"] = FROMDATE;
            dwData["WBBFLZZUGDAT"] = ENDDATE;
            try
            {
                Result = ((VWBFEHL)MV).QueryWBFEHL(dwData, 3);
                MergeData(Result, true, true);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

            base.QueryCommand(sender, e);
        }
        private void FROM_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                FROMDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }
        private void END_DATE_SelectedDateChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                ENDDATE = Helpers.Helper.GetDate(sender);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void ClearFilter_Click(object sender, RoutedEventArgs e)
        {
            cvMaster.CustomFilter = null;
        }

        private void Filter_Click(object sender, RoutedEventArgs e)
        {
            filter = "ARTICLE LIKE '" + txtART.Text.ToString().Trim() + "%'";
            cvMaster.CustomFilter = filter;
        }
        private void Print()
        {

            try
            {
                if (ITXEXRPT == null)
                { 
                    ITXEXRPT = new ITXEXRPT();
                    ITXEXRPT.Root = "C:\\IntexReport";
                    ITXEXRPT.strCompany = app.company;
                    ITXEXRPT.PrintType = "OpenExcel";
                }
                ITXEXRPT.dsSource = MV.SetProd;
                //ITXEXRPT.dsSource.Merge(SetEnv);
                if (IsChkArt)
                {
                    ITXEXRPT.DocType = "JLTRWB18DF";
                }
                else
                {
                    ITXEXRPT.DocType = "JLTRWB18D";
                }
                ITXEXRPT.strExcelFile = @"\JLT\JLTView.xls";
                string filename = ITXEXRPT.WriteExcelReport();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }

        }

        private void PrintCommand(object sender, RoutedEventArgs e)
        {
            Print();
        }
    }
}
